/*    */ package com.zimbra.cs.security.sasl;
/*    */ 
/*    */ import com.zimbra.cs.server.NioUtil;
/*    */ import java.nio.ByteBuffer;
/*    */ import javax.security.sasl.SaslException;
/*    */ import org.apache.mina.core.buffer.IoBuffer;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class SaslOutputBuffer
/*    */ {
/*    */   private final int mMaxSize;
/*    */   private ByteBuffer mBuffer;
/*    */   private static final int MINSIZE = 512;
/*    */   
/*    */   public SaslOutputBuffer(int maxSize)
/*    */   {
/* 35 */     this(Math.min(512, maxSize), maxSize);
/*    */   }
/*    */   
/*    */   public SaslOutputBuffer(int minSize, int maxSize) {
/* 39 */     if (minSize > maxSize) {
/* 40 */       throw new IllegalArgumentException("minSize > maxSize");
/*    */     }
/* 42 */     this.mBuffer = ByteBuffer.allocate(minSize);
/* 43 */     this.mMaxSize = maxSize;
/*    */   }
/*    */   
/*    */   public void put(IoBuffer buf) {
/* 47 */     put(buf.buf());
/*    */   }
/*    */   
/*    */   public void put(ByteBuffer bb) {
/* 51 */     if (isFull()) return;
/* 52 */     if (bb.remaining() > this.mBuffer.remaining()) {
/* 53 */       int minSize = Math.min(bb.remaining(), this.mMaxSize);
/* 54 */       this.mBuffer = NioUtil.expand(this.mBuffer, minSize, this.mMaxSize);
/*    */     }
/* 56 */     int len = Math.min(this.mBuffer.remaining(), bb.remaining());
/* 57 */     int pos = this.mBuffer.position();
/* 58 */     bb.get(this.mBuffer.array(), pos, len);
/* 59 */     this.mBuffer.position(pos + len);
/*    */   }
/*    */   
/*    */   public void put(byte b) {
/* 63 */     if (isFull()) return;
/* 64 */     if (!this.mBuffer.hasRemaining()) {
/* 65 */       this.mBuffer = NioUtil.expand(this.mBuffer, 1, this.mMaxSize);
/*    */     }
/* 67 */     this.mBuffer.put(b);
/*    */   }
/*    */   
/*    */   public int size() {
/* 71 */     return this.mBuffer.position();
/*    */   }
/*    */   
/*    */   public boolean isFull() {
/* 75 */     return this.mBuffer.position() >= this.mMaxSize;
/*    */   }
/*    */   
/*    */   public byte[] wrap(SaslSecurityLayer security) throws SaslException {
/* 79 */     return security.wrap(this.mBuffer.array(), 0, this.mBuffer.position());
/*    */   }
/*    */   
/*    */   public void clear() {
/* 83 */     this.mBuffer.clear();
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/security/sasl/SaslOutputBuffer.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */